home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / Example Scripts / RandomSample.vu < prev    next >
Encoding:
Text File  |  1998-06-04  |  2.8 KB  |  94 lines  |  [TEXT/MPS ]

  1. #
  2. #    File:        RandomSample.vu
  3. #
  4. #    Contains:    Example code to help describe the new random number
  5. #                seeding feature.
  6. #
  7. #    Written by:    Chad Williams
  8. #
  9. #    Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  10. #
  11. #    Change History (most recent first):
  12. #
  13. #                  9/3/92    CMW        Created
  14. #
  15.  
  16. (************************************************************************************
  17. * Script RandomSample( seed )
  18. *    This script will make a number of calls to the random number generator, including 
  19. *    getting and setting the seed values.  If a User provides a value for the 'seed'
  20. *    parameter to the script, the random number generator will be seeded with that
  21. *    value.
  22. *    Note that the seed can be set without changing the script itself!
  23. ************************************************************************************)
  24. Script RandomSample( seed )
  25. begin
  26.  
  27.     println;    
  28.  
  29.     # Check to see if the User provided a starting seed.
  30.     if( isundefined(seed) )
  31.     begin
  32.         # No seed provided, so print out the given one.
  33.         #    When RandomSeed() is called with no parameters, the random
  34.         #    number generator is left untouched.
  35.         #    RandomSeed() ALWAYS returns the value of the last seed it had
  36.         #    (in this case, the default seed).
  37.         x := RandomSeed();
  38.         println "Default Initial Seed: ", x;
  39.     end;
  40.     else
  41.     begin
  42.         # The User provided a seed.
  43.         # Use it, and log the value used.
  44.         RandomSeed( seed );
  45.         x := RandomSeed();
  46.  
  47.         # Note that 'x' should be equal to 'seed'.
  48.         println "Random Seed initialized by User to: ", x;
  49.     end;
  50.     
  51.     println;
  52.     println "# Random number tests:";
  53.  
  54.     println;
  55.     println "Random( 0, 10 ): ", Random( 0, 10 );
  56.  
  57.     # Note that negative numbers are now legal for the Random range.
  58.     println "Random( -10, 10 ): ", Random( -10, 10 );
  59.     println "Random( -10, 10 ): ", Random( -10, 10 );
  60.     println "Random( -10, 10 ): ", Random( -10, 10 );
  61.     println;
  62.  
  63.  
  64.     # The "Old Seed" will be equal to 'x', the original seed for the
  65.     #    random number generator.
  66.     println "Old Seed: ", RandomSeed( 12 );
  67.  
  68.     # The "New Seed" will be equal to 12, the last number the
  69.     #    random number generator was seeded with.
  70.     println "New Seed: ", RandomSeed();
  71.     println;
  72.     println "Random( 0, 10 ): ", Random( 0, 10 );
  73.     println "Random( -10, 10 ): ", Random( -10, 10 );
  74.     println "Random( -10, 10 ): ", Random( -10, 10 );
  75.     println "Random( -10, 10 ): ", Random( -10, 10 );
  76.     println;
  77.  
  78.  
  79.     # Now we will reseed the random number generator with the
  80.     #    initial seed so to demonstrate that the sequence of numbers
  81.     #    given by the random number generator depends only on the
  82.     #    seed.
  83.     #
  84.     # This sequence should exactly match the first sequence we printed:
  85.     println "Old Seed: ", RandomSeed( x );
  86.     println "New Seed: ", RandomSeed();
  87.     println;
  88.     println "Random( 0, 10 ): ", Random( 0, 10 );
  89.     println "Random( -10, 10 ): ", Random( -10, 10 );
  90.     println "Random( -10, 10 ): ", Random( -10, 10 );
  91.     println "Random( -10, 10 ): ", Random( -10, 10 );
  92.  
  93.  
  94. end;